<?php
namespace Home\Model;
use Think\Model;
/**
 * 权限规则model
 */
class GroupModel extends Model{

	/**
	 * 初始化
	 */
	public function __construct(){

	}

	/**
	 * 首页
	 */
	public function index(){

		$id = I("get.id",'','int');

		if(!empty($id)){

			//获取用户UID
			$map['a.id'] = array("eq",$id);

			$user_info = M("addpage_info")->alias("a")->field("a.user_id,a.typeid,b.qq")->join("LEFT JOIN __USERS__ b ON a.user_id = b.id")->where($map)->find();

			if($user_info['typeid'] == 2){

				//获取图文设置
				$where = array();
				$where['typeid'] = array("in",array(1,2,3));
				$where['addpage_info_id'] = array("eq",$id);
				$where['user_id'] = array("eq",$user_info['user_id']);
				$images_serial_list = M("addpage_details_images_serial")->field("id,typeid,class_id")->where($where)->order("id asc")->select();

				$images_text = array();

				foreach($images_serial_list as $k=>$list){

					$images_text[$k]['typeid'] = $list['typeid'];

					if($list['typeid'] == 1){
						$images_text[$k]['images'] = M("addpage_details_images")->field("image")->where("images_serial_id=".$list['id'])->order("id asc")->select();
					}else if($list['typeid'] == 2){
						$images_text[$k]['text'] = M("addpage_details_text")->where("images_serial_id=".$list['id'])->order("id asc")->getField("text");
					}else{
						$addpage_group = M("addpage_group")->alias("a")->field("a.addpage_info_id,b.page_img")->join("LEFT JOIN __ADDPAGE_INFO__ b ON a.addpage_info_id = b.id")->where("a.images_serial_id=".$list['id'])->order("a.id asc")->select();
						$images_text[$k]['addpage_group'] = $addpage_group;
						$images_text[$k]['class_id'] = $list['class_id'];
					}

				}

				//获取基本信息 - 售后电话
				$where = array();
				$where['id'] = array("eq",$id);
				$where['user_id'] = array("eq",$user_info['user_id']);
				$addpage_info = M("addpage_info")->field("page_name,page_title,as_phone,page_state")->where($where)->find();

				$data_time = time();

				$isMobile = isMobile();

				$data = array(
					"id"=>$id,
					"images_text"=>$images_text,
					"addpage_info"=>$addpage_info,
					"data_time"=>$data_time,
					"qq"=>$user_info['qq'],
					"ismobile"=>$isMobile,
					"code"=>200,
					"msg"=>"OK"
				);

			}else{

				$data = array(
					"code"=>401,
					"msg"=>"访问链接出错!"
				);

			}

		}else{

			$data = array(
				"code"=>400,
				"msg"=>"系统繁忙，请稍后重试!"
			);

		}

		return $data;

	}

}
